package org.dromara.system.controller.system;

import java.util.List;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.bo.SysProfitRuleBo;
import org.dromara.system.domain.query.SysProfitRuleQuery;
import org.dromara.system.domain.vo.SysProfitRuleVo;
import org.dromara.system.service.ISysProfitRuleService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 分佣规则
 *
 * @author yixiacoco
 * @date 2025-09-04
 */
@Validated
@RestController
@RequestMapping("/system/profitRule")
public class SysProfitRuleController extends BaseController {

    @Autowired
    private ISysProfitRuleService sysProfitRuleService;

    /**
     * 查询分佣规则列表
     */
    @SaCheckPermission("system:profitRule:list")
    @GetMapping("/list")
    public TableDataInfo<SysProfitRuleVo> list(SysProfitRuleQuery query) {
        return sysProfitRuleService.queryPageList(query);
    }

    /**
     * 导出分佣规则列表
     */
    @SaCheckPermission("system:profitRule:export")
    @Log(title = "分佣规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysProfitRuleQuery query, HttpServletResponse response) {
        List<SysProfitRuleVo> list = sysProfitRuleService.queryList(query);
        ExcelUtil.exportExcel(list, "分佣规则", SysProfitRuleVo.class, response);
    }

    /**
     * 获取分佣规则详细信息
     *
     * @param ruleId 主键
     */
    @SaCheckPermission(value = {"system:profitRule:query", "system:profitRule:edit"}, mode = SaMode.OR)
    @GetMapping("/{ruleId}")
    public R<SysProfitRuleVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable String ruleId) {
        return R.ok(sysProfitRuleService.queryById(ruleId));
    }

    /**
     * 新增分佣规则
     */
    @SaCheckPermission("system:profitRule:add")
    @Log(title = "分佣规则", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysProfitRuleBo bo) {
        return toAjax(sysProfitRuleService.insertByBo(bo));
    }

    /**
     * 修改分佣规则
     */
    @SaCheckPermission("system:profitRule:edit")
    @Log(title = "分佣规则", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysProfitRuleBo bo) {
        return toAjax(sysProfitRuleService.updateByBo(bo));
    }

    /**
     * 删除分佣规则
     *
     * @param ruleIds 主键串
     */
    @SaCheckPermission("system:profitRule:remove")
    @Log(title = "分佣规则", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ruleIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] ruleIds) {
        return toAjax(sysProfitRuleService.deleteWithValidByIds(List.of(ruleIds)));
    }
}
